import asyncio
from aiohttp import ClientSession
from bs4 import BeautifulSoup
import csv
async def getData(url,headers):
    cookies={'dper':'109fa70019dd15f41c67da825d854910d3e98f3f4613cf7f52c90ee217f58e3c203bff763937b831c987b1d533b62554b95ca99ba6cd4b747cccc6dba6aae323',
             '_hc.v':'1196b419-ea88-7b30-fa08-0d5be020df9d.1598878100',
             'uamo':'15622305301',
             'Hm_lvt_602b80cf8079ae6591966cc70a3940e7':'1598790322,1598790491,1598877729,1598877843',
             'Hm_lpvt_602b80cf8079ae6591966cc70a3940e7':'1598878113',
             '_lxsdk_cuid':'16cb91a5b13c8-007253e249ed82-37c153e-1fa400-16cb91a5b13c8',
             '_lxsdk':'16cb91a5b13c8-007253e249ed82-37c153e-1fa400-16cb91a5b13c8',
             '_lxsdk_s':'174448ed185-ae9-dac-37%7C%7C52',
             'cy':'4',
             'll':'7fd06e815b796be3df069dec7836c3df',
             'ua':'WangJL',
             'dplet':'a6585f3f03e2e47b71a8adcbe5fd6197',
             'cye':'guangzhou',
             'fspop':'test'}
    async with ClientSession(cookies=cookies) as session:
        async with session.get(url,headers=headers,) as response:
            return await response.text()

def saveData(result):
    for i in result:
        soup=BeautifulSoup(i,'html.parser')
        find_div=soup.find_all('div',class_='tit')
        for d in find_div:
            a =d.find('a')
            name=d.find('h4').getText()
            csvFile=open('c://Users//Administrator//Desktop//data.csv','a',newline='',encoding='utf-8')
            writer=csv.writer(csvFile)
            writer.writerow(name)
            csvFile.close()
def run():
    for i in range(1,51):
        task=asyncio.ensure_future(getData(url.format(i),headers))
        tasks.append(task)
    result=loop.run_until_complete(asyncio.gather(*tasks))
    saveData(result)
    print(len(result))
if __name__ == '__main__':
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'}
    tasks=[]
    url="http://www.dianping.com/guangzhou/ch10/p{}"
    loop=asyncio.get_event_loop()
    run()